#!/bin/sh

#
# Test generating the size of a file resource
#

. "$(cd "$(dirname "$0")" && pwd)/common.sh"

EXPECTED_SIZE_FILE=$WORK/expected_size.txt

cat >$EXPECTED_SIZE_FILE <<EOF
1024
EOF

cat >$CONFIG <<EOF
file-resource some/random-file.ext {
    host-path = "${TESTFILE_1K}"
}
file-resource size.txt {
    contents = "\${FWUP_SIZE_some_random_file_ext}\n"
}
task complete {
    on-resource size.txt {
        raw_write(0)
    }
}
EOF

# Create the firmware file like normal
$FWUP_CREATE -c -f $CONFIG -o $FWFILE

# Apply the firmware and verify that the correct size was generated
$FWUP_APPLY -a -d $IMGFILE -i $FWFILE -t complete

# Since fwup writes in chunks, trim the image file for cmp.
dd if=$IMGFILE of=$IMGFILE.trimmed bs=1 count=5 2>/dev/null
cmp $EXPECTED_SIZE_FILE $IMGFILE.trimmed
